<?php
include_once('./config/config.php');
$title = '部门';
#搜索start
// 定义一个变量用于sql条件查询
$where = 1;
// 获取输入的关键字
$keyword = $_GET["keyword"];
if (!empty($keyword)) {
    $where = "`name` like '%$keyword%' AND ";
}
// 删除右边的 'AND '
$where = rtrim($where, ' AND ');
#搜索end

#分页start
// 获取员工数据的总数
$CountSql = "SELECT count(*) as count FROM `pre_department` where $where";
$Count = find($CountSql);
// 获取当前页
$page = !empty($_GET['page']) ? $_GET['page'] : 1;
// 每页显示多少条数据
$limit = 5;
// 每页显示多少个分页的页码数
$size = 5;
// 调用分页函数
$ShowPage = page($page, $Count['count'], $limit, $size);
$start = ($page - 1) * $limit;
#分页end

// 查询管理员表所有的用户及其信息
$sql = "select * from `pre_department` where $where LIMIT $start,$limit";
$DepList = all($sql);
?>


<!DOCTYPE html>
<html lang="en">

<head>
    <!-- 引入公共样式 -->
    <?php include_once('./common/meta.php'); ?>

    <!-- 引入公共js -->
    <?php include_once('./common/script.php'); ?>
</head>
<style>
    input[value="搜索"] {
        margin-top: -10px;
    }
</style>

<body>

    <!-- 引入头部 -->
    <?php include_once('./common/header.php'); ?>

    <!-- 引入侧边菜单 -->
    <?php include_once('./common/menu.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">部门列表</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.php">Home</a> <span class="divider">/</span></li>
            <li class="active">Index</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='DepAdd.php'"><i class="icon-plus"></i>新增</button>
                </div>
                <div>
                    <form method="GET">
                        <input type="search" name="keyword" value="<?php echo $keyword; ?>" placeholder="请输入要搜索的部门名称">
                        <input class="btn btn-primary" type="submit" value="搜索">
                    </form>
                </div>
                <div class="well">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>部门名称</th>
                                <th style="width: 26px;"></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($DepList as $item) { ?>
                                <tr>
                                    <td><?php echo $item['id']; ?></td>
                                    <td><?php echo $item['name']; ?></td>
                                    <td>
                                        <a href="DepEdit.php?id=<?php echo $item['id']; ?>"><i class="icon-pencil"></i></a>
                                        <a href="#myModal" role="button" data-toggle="modal" data-id="<?php echo $item['id']; ?>" class="delete"><i class="icon-remove"></i></a>
                                    </td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
                <?php echo $ShowPage; ?>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">Delete Confirmation</h3>
                    </div>
                    <div class="modal-body">
                        <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?</p>
                    </div>
                    <div class="modal-footer">
                        <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                        <button class="btn btn-danger" data-id="" id="delete" data-dismiss="modal">Delete</button>
                    </div>
                </div>

                <!-- 引入底部 -->
                <?php include_once('./common/footer.php'); ?>
            </div>
        </div>
    </div>

</body>

</html>
<script>
    // 删除按钮
    $('.delete').click(function() {
        var id = $(this).data('id');
        $('#delete').data('id', id);
    })

    // 弹窗的delete
    $('#delete').click(function() {
        var id = $(this).data('id');
        $.ajax({
            type: "POST",
            url: 'proces.php',
            data: {
                action: 'depdelete',
                id,
            },
            dataType: 'json',
            success: function(data) {
                if (data.code === 1) {
                    layer.msg(data.msg, {
                        icon: 1,
                        time: 600
                    }, function() {
                        location.reload();
                    });
                } else {
                    layer.msg(data.msg);
                }
            }
        })
    });
</script>